﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _01_简单工厂
{

    public abstract class Operation
    {
        /// <summary>
        /// 左操作数
        /// </summary>
        public double NumberA { get; set; }
        /// <summary>
        /// 右操作数
        /// </summary>
        public double NumberB { get; set; }
        /// <summary>
        /// 计算结果
        /// </summary>
        /// <returns></returns>
        public abstract double GetResult();
    }

    //构建派生类:OperationAdd
    public class OperationAdd : Operation
    {
        public override double GetResult() => NumberA + NumberB;
    }

    //构建派生类:OperationSub
    public class OperationSub : Operation
    {
        public override double GetResult() => NumberA - NumberB;
    }
    //构建派生类:OperationMul
    public class OperationMul : Operation
    {
        public override double GetResult() => NumberA * NumberB;
    }
    //构建派生类:OperationDiv
    public class OperationDiv : Operation
    {
        public override double GetResult() => NumberA / NumberB;
    }


    public class OperationFactory
    {
        public static Operation CreateOperationByName(string name)
        {
            Operation oper = null;
            switch (name)
            {
                case "+":
                    oper = new OperationAdd();
                    break;
                case "-":
                    oper = new OperationSub();
                    break;
                case "*":
                    oper = new OperationMul();
                    break;
                case "/":
                    oper = new OperationDiv();
                    break;
            }
            return oper;
        }
    }

    internal class Program
    {
        static void Main(string[] args)
        {
            //提示用户输入数值
            Console.Write("请输入数值:");
            string strNumberA = Console.ReadLine();

            Console.Write("请输入数值:");
            string strNumberB = Console.ReadLine();

            //提示用户输入运算符
            Console.Write("请输入运算符(+、-、*、/):");
            string StrOper = Console.ReadLine();

            //简单工厂->根据输入的字符串返回不同的对象
            Operation operation = OperationFactory.CreateOperationByName(StrOper);

            //对属性进行赋值
            operation.NumberA = Convert.ToDouble(strNumberA);
            operation.NumberB = Convert.ToDouble(strNumberB);

            //打印计算结果
            Console.WriteLine("{0}{1}{2}={3}", operation.NumberA,StrOper, operation.NumberB,operation.GetResult());

            Console.ReadKey();
        }
    }
}
